﻿@{
    ViewBag.Title = "Edit";

    String heading = "";
    String name = ViewData["name"] != null ? ViewData["name"].ToString() : "";
    String id = ViewData["id"] != null ? ViewData["id"].ToString() : "";
    String target = ViewData["target"] != null ? ViewData["target"].ToString() : "";
    String mode = ViewData["mode"] != null ? ViewData["mode"].ToString() : "";
    String loadscripts = ViewData["loadscripts"] != null ? ViewData["loadscripts"].ToString() : "";
    String embedded = ViewData["embedded"] != null ? ViewData["embedded"].ToString() : "";

    //String loadscripts = ViewData["loadscripts"].ToString();
    if ((!String.IsNullOrEmpty(embedded) && bool.Parse(embedded)))
    //(!String.IsNullOrEmpty(loadscripts) && bool.Parse(loadscripts)))
    {
    <script type="text/javascript" src="/Scripts/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="/Scripts/DataManager.js"></script>
    <script type="text/javascript" src="/Scripts/jquery.grideditor.js"></script>
    }

    if (ViewData["mode"] == "Create")
    {
        heading = "Create a new " + name + " entry";
    }
    else if (!String.IsNullOrEmpty(id))
    {
        heading = "Edit " + name + " entry with id:" + id;
    }
    
}
<script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
<script>
    $(document).ready(function () {

        $('#@target').find('.cancel-button').bind('click', function () {
            fadeAndCloseEditor('#@target form.edit-form');
        });
        getData('/m/sp_getTableHeader', 'contentname=@name',
            function (data) {
                var contentname = '@name';
                var contentid = '@id';
                var targetdiv = '@target';
                var mode = '@mode';
                var storedProcMode = '@mode';
                var loadscripts = '@loadscripts';
                var targetForm = '#' + targetdiv + " form.edit-form";

                if (mode == "Edit")
                    storedProcMode = "Update";
                else if (mode == "Create")
                    storedProcMode = "Insert";

                if (contentname == "") {
                    targetForm.hide();
                    return;
                }
                $('#' + targetdiv).epicEditor({ data: data, contentname: contentname, contentid: contentid, mode: mode, storedProcMode: storedProcMode, targetForm: targetForm, successSaveFun: successSave });
            }, false);
    });

    function fadeAndCloseEditor(target) {
        var formdiv = $(target).parents('.outer-div').parent();
        formdiv.fadeOut('slow', function () {
            $(this).remove();
        });
    }

    function successSave(savedData, target) {

        if (savedData.error) {
            alert('There was an error saving the data. \nMessage: "' + savedData.error);
        }
        else {
            fadeAndCloseEditor(target);
        }

    }
</script>
<div class="outer-div">
    <div id="@target" class="editor-box">
        <h4>@heading</h4>
        <form class="edit-form">
        <table>
            <tr>
                <td class="name">
                    <input type="button" class="grey-item image-button cancel-button" tip="Cancel" value="cancel">
                </td>
                <td class="value">
                    <input type="submit" class="grey-item image-button save-button" tip="Save" value="save">
                </td>
            </tr>
        </table>
        </form>
    </div>
</div>
